package com.smartplatform.controller;

import com.smartplatform.dto.R;
import com.smartplatform.dto.UserDto;
import com.smartplatform.pojo.SysUserInfo;
import com.smartplatform.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author 咕唧
 * @Date 2022/5/26 11:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/sysusers")
public class SysUserController {

    private SysUserService sysUserService;

    @Autowired
    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @PostMapping("/register")
    public R register(@RequestBody UserDto userDto) {
//        System.out.println("SysUserController的register方法执行了");
        sysUserService.addUser(userDto);
        //注册成功
        return R.setOK();
    }

    //@PostMapping("/login")
    public R login(UserDto userDto, HttpSession session) {
        SysUserInfo userInfo = sysUserService.login(userDto);
        //登录成功放入session中
        session.setAttribute("user", userInfo);
        return R.setOK();
    }

    /**
     * 用户修改信息
     * @param userDto
     * @return
     */
    @PostMapping("/modify")
    public R modify(@RequestBody UserDto userDto) {
        sysUserService.modify(userDto);
        return R.setOK();
    }

    /**
     * 查看用户数据
     * @return
     */
    @GetMapping("/view")
    public R selectUser() {
        return R.setOK(sysUserService.selectUser());
    }

}
